package com.winning.dps.ds.common;

import common.entity.CommonJgUserEntity;
import common.exception.BaseException;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.springframework.http.HttpStatus;
import org.springframework.validation.BindingResult;

import java.util.HashMap;
import java.util.Map;

/**
 * @program: plat
 * @description:  controller 基类
 * @author: nxs
 * @create: 2018-07-13 23:07
 **/
@Slf4j
public abstract class BaseController {


    //当前登录操作人员
    protected CommonJgUserEntity getUser() {
        return (CommonJgUserEntity) SecurityUtils.getSubject().getPrincipal();
    }

    //注册流水号
    protected String getZclsh() {
        return getUser().getZclsh();
    }

    //当前登录--机构编码
    protected String getJgbm() {
        return getUser().getJgbm();
    }

    public void checkValid(BindingResult bindingResult){
        if(bindingResult.hasErrors()){
            throw  new BaseException(bindingResult.getFieldError().getDefaultMessage(), HttpStatus.BAD_REQUEST.value());
        }
    }


    /**
     * 成功返回格式
     * @param value
     * @param repData
     * @return
     */
    public static Map<String,Object> successObjectResult(Object value, Object repData) {
        Map<String, Object> result = new HashMap<>(3);
        result.put("statusCode", 200);
        result.put("repData",repData);
        result.put("repMsg", value);
        return result;
    }


    /**
     * 失败返回格式
     * @param repMsg
     * @param code
     * @return
     */
    public static Map<String,Object> baseError(String repMsg,int code) {
        Map<String, Object> result = new HashMap<>(3);
        result.put("statusCode", code);
        result.put("repMsg", repMsg);
        result.put("repData","");
        return result;
    }
}
